/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.api.matedata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @author huhailiang937
 * @date 2017年7月5日下午6:56:17
 * @since 1.0.0
 */
public abstract class MetaCacheSupport {

    public static final String ZK_CONFIG_KEY = "seachcloud.meta.zk-host";
    
    public static final String ROOT_ROUTE_PATH = "/hfregister/route";

    public static final String ROOT_ROUTE_KEY = "seachcloud_hfregister:route";

    public static final String ROUTE_FILE = ROOT_ROUTE_PATH + "/%s-endpoint.json";
    
    public static final String ROOT_META_PATH = "/hfregister/meta";

    public static final String ROOT_META_KEY = "seachcloud_hfregister:meta";

    public static final String METADATA_FILE = ROOT_META_PATH + "/%s-metadata.json";
    
    public static <T> T decode(byte[] data, Class<T> clazz) {
        return JSON.parseObject(new String(data), clazz);
    }

    public static <T> T decode4Json(String data, Class<T> clazz) {
        return JSON.parseObject(new String(data), clazz);
    }

    public static byte[] encode(Object bean) {
        return JSON.toJSONBytes(bean, SerializerFeature.IgnoreNonFieldGetter);
    }

    public static String encode2Json(Object bean) {
        return JSON.toJSONString(bean, SerializerFeature.IgnoreNonFieldGetter);
    }

    public static String getZKRoutePath(String routeKey) {
        return String.format(ROUTE_FILE, routeKey);
    }
    
    public static String getZKMetaPath(String routeKey) {
        return String.format(METADATA_FILE, routeKey);
    }
}
